$(function () {
    $("#salary_grid").datagrid({
        /*自适应*/
        fit: true,
        /*向此地址发送请求获得数据填充表格*/
        url: '/salary/query.do',
        columns: [/*此处为第一层列标题*/[
            {
                field: "employee", title: "员工姓名", width: 100, formatter: function (value) {
                    return value ? value.username : ''
                }
            },
            {
                field: "employee_dept", title: "部门", width: 100, formatter: function (value, row) {
                    return row.employee.dept ? row.employee.dept.name : ""
                }
            },
            {
                field: "employee_tel", title: "电话", width: 100, formatter: function (value, row) {
                    return row.employee.tel ? row.employee.tel : ""
                }
            },
            {
                field: "employee_email", title: "邮箱", width: 100, formatter: function (value, row) {
                    return row.employee.email ? row.employee.email : ""
                }
            },
            {
                field: "salary", title: "基本工资", width: 70
            },
            {
                field: "bonus", title: "奖金", width: 70
            },
            {
                field: "cutsalary", title: "扣薪", width: 70
            },
            {
                field: "paydate", title: "发薪日", width: 100
            },
            {
                field: "paydate", title: "总薪资", width: 100
            },
            {
                field: "a", title: "查看薪资明细", width: 100,formatter:function () {
                    return "<a href='javascript:check()'><p style='color: #56C9F5'>查看</p></a>"
                }
            }
        ]/*此处可以插入第二层标题*/],
        //为表格顶端的按键
        toolbar: "#tb",
        //真正的自动展开收缩列的大小,以适应网格的宽度,防水平滚动
        fitColumns: true,
        //可在行底显示分页工具栏
        pagination: true,
        //行码
        rownumbers: true,
        //只允许单选某行
        singleSelect: true
    })
    //对弹窗进行初始化
    $("#salary_dialog").dialog({
        width: 350,
        height: 450,
        resizable: true,
        buttons: '#form_btn',
        closed: true,
        onClose:function () {
            $("#salary_form").form("clear")
            $('#btn').linkbutton('enable');
        }

    })
})
function opendialog() {
    $("#username").show();
    var salary
    var bonus
    var cutSalary

    $("#salary_dialog").dialog('setTitle', '新建薪资单');
    $("#salary_dialog").dialog('open');


       $('#salary').numberbox({
        onChange: function(value) {
            count()
        }
    });
    $("#bonus").numberbox({
        onChange:function (value) {
            count()
        }
    })
    $("#cutsalary").numberbox({
        onChange:function (value) {
            count()
        }
    })
    $(".a").numberbox('readonly',false)
    $(".b").combobox('readonly',false)
    $(".c").datebox('readonly',false)
    $('#btn').linkbutton('enable');

}

function reload() {
    $("#salary_grid").datagrid('reload');
    $("#statebtn").linkbutton({
        disabled:false
    })
    $("#editbtn").linkbutton({
        disabled:false
    })
}

//表单中的按键
function query() {
    var keyword = $("[name=keyword]").val();
    var deptId = $("[name=deptId]").val();
    var startTime = $("[name=startTime]").val();
    var finalTime = $("[name=finalTime]").val();
    $("#salary_grid").datagrid('load', {
        keyword: keyword,
        deptId: deptId,
        startTime: startTime,
        finalTime: finalTime

    })
}
function save() {

    $("#salary_form").form('submit', {
        url: '/salary/saveOrUpdate.do',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                    cancel();
                    $("#salary_grid").datagrid('reload');
                });
            } else {
                $.messager.alert('温馨提示', data.msg, 'warning');
            }
        }
    })
}

function edit() {
    $("#username").hide()
    var row = $("#salary_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    if(row.employee){
        row["employee.id"] =row.employee.id;
    }

    $("#salary_form").form('load',row);
    $("#salary_dialog").dialog('setTitle', '编辑薪资单');
    $("#salary_dialog").dialog('open');

    var salary
    var bonus
    var cutSalary
    $('#salary').numberbox({
        onChange: function(value) {
            count()
        }
    });
    $("#bonus").numberbox({
        onChange:function (value) {
            count()
        }
    })
    $("#cutsalary").numberbox({
        onChange:function (value) {
            count()
        }
    })
    $(".a").numberbox('readonly',false)
    $(".b").combobox('readonly',false)
    $(".c").datebox('readonly',false)
    $('#btn').linkbutton('enable');

}
function cancel() {
    $("#salary_dialog").dialog('close');
}

function check() {
    var row = $("#salary_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    $('#btn').linkbutton('disable');
    $("#username").hide()
    $("#salary_form").form('load',row);
    $("#salary_dialog").dialog('setTitle', '薪资详单');
    $("#salary_dialog").dialog('open');
    $(".a").numberbox('readonly');
    $(".b").combobox('readonly')
    $(".c").datebox('readonly')
}
function exportXls() {
    window.location.href="/salary/exportXls.do";
}
function count() {
    var salary
    var bonus
    var cutSalary
    salary = $("#salary").numberbox('getValue');
    if(salary==null){
        salary=0
    }
    salary= parseInt(salary)
    $("#totalsalary").numberbox('setValue',salary)
    bonus = $("#bonus").numberbox('getValue');
    if(bonus==null){
        bonus=0
    }
    bonus= parseInt(bonus)
    $("#totalsalary").numberbox('setValue',salary+bonus)
    cutSalary = $("#cutsalary").numberbox('getValue');
    if(cutSalary==null){
        cutSalary=0
    }
    cutSalary= parseInt(cutSalary)
    $("#totalsalary").numberbox('setValue',salary+bonus-cutSalary)
}
function deletesalary() {
    var row = $("#salary_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    $.post("/salary/delete.do",{id:row.id},function (data) {
        if(data.success){
            $.messager.alert("温馨提示","删除成功","info",function () {
                reload()
            })
        }else {
            $.messager.alert("温馨提示",data.msg,"warning")
        }
    })
}